package com.sgb.jicai.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.sgb.jicai.BR;
import com.sgb.jicai.R;
import com.sgb.jicai.entity.PriceRulesBean;
import com.sgb.link.utils.UnitListUtils;
import com.swgk.core.customview.DecimalInputTextWatcher;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * Time:2021/7/23
 * AUTHOR:F_Q
 * Description:
 */
public class ProcurementCountPriceAdapter extends BaseRecycleAdapter<PriceRulesBean> {

    private String unitNames = "";
    private ProcurementCountPriceCallback callback;

    public void setCallback(ProcurementCountPriceCallback callback) {
        this.callback = callback;
    }

    public ProcurementCountPriceAdapter(Context context, List<PriceRulesBean> datas) {
        super(context, R.layout.item_procurement_count_price, datas);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        holder.itemView.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.ProcurementCountPriceDeleta(position);
            }
        });
        EditText startvalue = holder.itemView.findViewById(R.id.start_value);
        EditText end_value = holder.itemView.findViewById(R.id.end_value);
        ItemInputTextView contact_name = holder.itemView.findViewById(R.id.contact_name);
        contact_name.setCheck(6, 2);
        contact_name.setInfilterType(ItemInputTextView.TYPE_NUMBER_DECIMAL);
        if (!UnitListUtils.getUnitStart(unitNames)) {
            startvalue.setInputType(InputType.TYPE_CLASS_NUMBER);
            startvalue.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            end_value.setInputType(InputType.TYPE_CLASS_NUMBER);
            end_value.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        } else {
            startvalue.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            startvalue.addTextChangedListener(new DecimalInputTextWatcher(startvalue, 6, 3));// 整数最多6位，小数最多3位
            end_value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            end_value.addTextChangedListener(new DecimalInputTextWatcher(startvalue, 6, 3));// 整数最多6位，小数最多3位
        }
        //只有一条数据时这样处理
        if (position==0&&position==datas.size()-1){
            startvalue.setFocusable(true);
            startvalue.requestFocus();
            startvalue.setFocusableInTouchMode(true);
            end_value.setFocusable(true);
            end_value.requestFocus();
            end_value.setFocusableInTouchMode(true);
        }
        //多条数据处理
        if (position >= 0&&position<datas.size()-1){
            startvalue.setFocusable(false);
            startvalue.setFocusableInTouchMode(false);
            end_value.setFocusable(false);
            end_value.setFocusableInTouchMode(false);
        }
        //判断是否是最后一条
        if (position>0&&position==datas.size()-1){
            startvalue.setFocusable(false);
            startvalue.setFocusableInTouchMode(false);
            end_value.setFocusable(true);
            end_value.requestFocus();
            end_value.setFocusableInTouchMode(true);
        }
    }

    @Override
    public void convert(ItemViewHolder holder, PriceRulesBean entity) {
        holder.setBinding(BR.viewModel, entity);

    }

    public void setUnit(String unitName) {
        unitNames = unitName;
    }

    public interface ProcurementCountPriceCallback {
        void ProcurementCountPriceDeleta(int position);
    }
}
